setwd("E:\\5hmc_file\\2_5hmc_yjp_bam\\ASM")
file=read.csv("./20201112做汇总表/all.FDR.sig.at.least.one.csv",head=T)
group1=c("X2B_X1T","M8_M7","M6_M5","M2_M1","M48_M47","M50_M49","M28_M27","M30_M29","M26_M25","M35_M36","M18_M17","M20_M19","M22_M21","M40_M39")
#file=file[!(file$num1==2&file$twins==1),]
#file=file[file$num1>1,]

file$pattern.nodupl.DC=NA
file$pattern.nodupl.num.DC=1
for(j in 1:dim(file)[1]){
  str1=unique(unlist(strsplit(file[j,"pattern1.DC"],":")))
  file[j,"pattern.nodupl.DC"]=paste(str1,collapse = ":")
  file[j,"pattern.nodupl.num.DC"]=length(str1)
}
file$pattern.nodupl.CC=NA
file$pattern.nodupl.num.CC=1
for(j in 1:dim(file)[1]){
  str1=unique(unlist(strsplit(file[j,"pattern1.CC"],":")))
  file[j,"pattern.nodupl.CC"]=paste(str1,collapse = ":")
  file[j,"pattern.nodupl.num.CC"]=length(str1)
}
file$pattern.nodupl.HC=NA
file$pattern.nodupl.num.HC=1
for(j in 1:dim(file)[1]){
  str1=unique(unlist(strsplit(file[j,"pattern1.HC"],":")))
  file[j,"pattern.nodupl.HC"]=paste(str1,collapse = ":")
  file[j,"pattern.nodupl.num.HC"]=length(str1)
}
file1=file[!file$pattern1.DC=="",]#only sig in DC
file1=file[file$DC.FDR.sig>0,]
file1=file1[file1$pattern.nodupl.num.DC==1,]
#file1=file1[file1$pattern.nodupl.num.CC==1,]


file1$pattern.nodupl.num.all=file1$pattern.nodupl.num.DC+file1$pattern.nodupl.num.CC+file1$pattern.nodupl.num.HC
file2=file1[file1$pattern.nodupl.num.all>1,]#
file2$id=paste(file2$Chr,file2$Start,sep=":")
bf=read.table("E:/5hmc_file/2_5hmc_yjp_bam/ASM/bayes_p/bias_AShM_BF_no_motif.txt",header = T,sep="\t")
bf=data.frame(id=bf$unitID,BF_in_DC=bf$BF_in_DC)
file2=merge(bf,file2,by="id")
#file2=file2[!file2$pattern.nodupl.CC=="onedown_oneup",]
#file2=file2[!file2$pattern.nodupl.HC=="onedown_oneup",]

file2$pattern.nodupl.CC.trans=""
for(j in 1:dim(file2)[1]){
  if(!stringr::str_detect(file2[j,"pattern.nodupl.CC"],pattern = "onedown_oneup")){
    str1=unique(unlist(strsplit(file2[j,"pattern.nodupl.CC"],":")))
    str1=gsub("one_is_down","tumor_down",str1)
    str1=gsub("one_is_up","tumor_up",str1)
    str1=gsub("normal_down_tumor_down","tumor_down",str1)
    str1=gsub("normal_up_tumor_up","tumor_up",str1)
    
    str1=unique(c(str1))
    file2[j,"pattern.nodupl.CC.trans"]=paste(str1,collapse = ":") 
  }else{file2[j,"pattern.nodupl.CC.trans"]="include.two.direction"}
}
file2$pattern.nodupl.HC.trans=""
for(j in 1:dim(file2)[1]){
  if(!stringr::str_detect(file2[j,"pattern.nodupl.HC"],pattern = "onedown_oneup")){
    str1=unique(unlist(strsplit(file2[j,"pattern.nodupl.HC"],":")))
    str1=gsub("one_is_down","normal_down",str1)
    str1=gsub("one_is_up","normal_up",str1)
    str1=gsub("normal_down_tumor_down","normal_down",str1)
    str1=gsub("normal_up_tumor_up","normal_up",str1)
    
    str1=unique(c(str1))
    file2[j,"pattern.nodupl.HC.trans"]=paste(str1,collapse = ":") 
  }else{file2[j,"pattern.nodupl.HC.trans"]="include.two.direction"}
}

file2$DC.pattern.valid.CC=""
file2$DC.pattern.valid.HC=""
for(j in 1:dim(file2)[1]){
  if(!file2[j,"pattern.nodupl.CC.trans"]==""){
    if(stringr::str_detect(file2[j,"pattern.nodupl.DC"],pattern = file2[j,"pattern.nodupl.CC.trans"])){
      file2[j,"DC.pattern.valid.CC"]="TRUE"
    }else{file2[j,"DC.pattern.valid.CC"]="FALSE"}
  }else{file2[j,"DC.pattern.valid.CC"]=""}
  
  if(!file2[j,"pattern.nodupl.HC.trans"]==""){
    if(stringr::str_detect(file2[j,"pattern.nodupl.DC"],pattern = file2[j,"pattern.nodupl.HC.trans"])){
      file2[j,"DC.pattern.valid.HC"]="TRUE"
    }else{file2[j,"DC.pattern.valid.HC"]="FALSE"}
  }else{file2[j,"DC.pattern.valid.HC"]=""}
}
file2$valid.num=rowSums(file2[,grep(names(file2),pattern = "DC.pattern.valid")]=="TRUE")
test=file2[file2$valid.num>1,]
sel=which(test$pattern.nodupl.DC=="normal_up-tumor_up"|test$pattern.nodupl.DC=="normal_down-tumor_down")
test=test[-sel,]
table(test$BF_in_DC>3)

sel=which(file2$DC.pattern.valid.CC=="FALSE"|file2$DC.pattern.valid.HC=="FALSE")
test=file2[-sel,]
test=test[test$valid.num==1,]
sel=which(test$pattern.nodupl.DC=="normal_up-tumor_up"|test$pattern.nodupl.DC=="normal_down-tumor_down")
test=test[-sel,]
table(test$BF_in_DC>3)
